package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private StudentMapper studentMapper;
    /**
     * 分页查询
     */
    @Override
    public PageResult<Clazz> findPage(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> clazzPage=new Page<>(page,pageSize);//设置分页参数

        clazzPage.addOrder(OrderItem.desc("update_time"));//设置排序规则
        //根据班级名称与结课时间条件查询
        LambdaQueryWrapper<Clazz> queryWrapper=new LambdaQueryWrapper<Clazz>()
                .like(!StringUtils.isEmpty(name),Clazz::getName,name)
                .between(begin != null && end != null,Clazz::getEndDate,begin,end);

        page(clazzPage,queryWrapper);
        List<Clazz> clazzList = clazzPage.getRecords();
        clazzList.forEach(clazz -> {
            Integer masterId = clazz.getMasterId();
            Emp emp = empMapper.selectById(masterId);
            if (emp != null){
                clazz.setMasterName(emp.getName());
            }
            //根据当前时间与结课时间设置"已结课"，“未开班”，“正在上课”
            if (clazz.getEndDate() != null){
                if (clazz.getEndDate().isBefore(LocalDate.now())){
                    clazz.setStatus("已结课");
                }else if (clazz.getBeginDate().isAfter(LocalDate.now())){
                    clazz.setStatus("未开班");
                }else {
                    clazz.setStatus("正在上课");
                }
            }
        });

        return new PageResult<>(clazzPage.getTotal(),clazzPage.getRecords());
    }

    /**
     * 新增班级
     */
    @Override
    public void saveClazz(Clazz clazz) {
        //新建创建时间与更新时间
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        save(clazz);
    }
    /**
     * 删除班级
     */

    @Override
    public void removeClazzById(Integer id) {
        //如果班级下有学员，不允许删除
        if (studentMapper.selectCount(new LambdaQueryWrapper<Student>().eq(Student::getClazzId,id)) > 0){
            throw new BusinessException("该班级下有学员，不允许删除");
        }
        removeById(id);
    }
}
